package com.loma.common.customUI


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CalendarView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.tabs.TabLayoutMediator
import com.loma.common.R
import com.loma.common.databinding.DateRangeDialogBinding
import com.loma.common.utils.ext.showToast
import com.loma.common.utils.ext.singleClick
import java.util.*


private const val ARG_PARAM1 = "param1"


class TimeRangeDialog(val callback:( begin:Date ,end:Date )-> Unit   ) : BottomSheetDialogFragment() {


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.date_range_dialog, container)

    }


    var binding: DateRangeDialogBinding? = null
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding = DataBindingUtil.bind<DateRangeDialogBinding>(view)


        binding?.let {
            val adapter = object : FragmentStateAdapter(this) {
                override fun getItemCount() = TAB_TITLES.size
                override fun createFragment(position: Int): Fragment {
                    if (position == 0) return Begin(vm)
                    else return End(vm)
                }
            }

            it.viewPager.adapter = adapter
            it.viewPager.offscreenPageLimit = 1

            it.viewPager.isUserInputEnabled = false
            val tabLayoutMediator =
                TabLayoutMediator(it.tabLayout, it.viewPager) { tab, position ->
                    tab.text = TAB_TITLES[position]
                }

            tabLayoutMediator.attach()

            it.buttonCancel.singleClick {
                dismiss()
            }
            it.buttonOk.singleClick {


                if (vm.begin.value == null) {
                    requireContext().showToast("请选择一个开始日期")
                } else if (vm.end.value == null) {
                    requireContext().showToast("请选择一个截至日期")
                } else{
                    callback.invoke(vm.begin.value!!,vm.end.value!!)
                    dismiss()

                }


            }
        }


    }


    var TAB_TITLES = arrayOf("开始日期", "结束日期")


    internal class Begin(val vm: VM) : Fragment(), CalendarView.OnDateChangeListener {
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            var datePicker = CalendarView(requireContext())
            datePicker.setOnDateChangeListener(this)
            return datePicker
        }

        override fun onSelectedDayChange(
            view: CalendarView,
            year: Int,
            month: Int,
            dayOfMonth: Int
        ) {
            var calendar = Calendar.getInstance()
            calendar.set(year, month, dayOfMonth)
            vm.begin.value = calendar.time


        }
    }

    internal class End(val vm: VM) : Fragment(), CalendarView.OnDateChangeListener {
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            var datePicker = CalendarView(requireContext())
            datePicker.setOnDateChangeListener(this)
            return datePicker
        }

        override fun onSelectedDayChange(
            view: CalendarView,
            year: Int,
            month: Int,
            dayOfMonth: Int
        ) {
            var calendar = Calendar.getInstance()
            calendar.set(year, month, dayOfMonth)
            vm.end.value = calendar.time


        }
    }

    val vm by lazy { ViewModelProvider(this).get(VM::class.java) }

    class VM : ViewModel() {

        var begin = MutableLiveData<Date>()
        var end = MutableLiveData<Date>()


    }


    companion object {

        @JvmStatic
        fun newInstance(callback: (begin: Date, end: Date) -> Unit) = TimeRangeDialog(callback = callback)
    }
}